package handler.server;

import handler.ChannelCach;
import io.netty.channel.ChannelHandler.Sharable;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;

import java.net.SocketAddress;

/**
 * @author dfj@99chuxing.com
 * @date 2018/3/29 0029 14:03
 */
@Sharable
public class ConnectHandler extends ChannelInboundHandlerAdapter {


    private static final ChannelCach CHANNEL_CACH = new ChannelCach();
    private static int a=0;

    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        System.out.println("建立连接-----记录Channel信息-----");
        a++;
        SocketAddress socketAddress = ctx.channel().remoteAddress();
        CHANNEL_CACH.put(socketAddress.toString(),ctx.channel());
        System.out.println("目前建立的通道数----"+CHANNEL_CACH.size());
    }

    @Override
    public void channelInactive(ChannelHandlerContext ctx) throws Exception {
        SocketAddress socketAddress = ctx.channel().remoteAddress();
        /** 删除缓存的已断开的通道*/
        CHANNEL_CACH.remove(socketAddress.toString());
        System.out.println(socketAddress.toString()+"--------关闭-----");

    }
}
